package org.beetl.json.loc;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.beetl.json.JsonTool;

public class IndexLocations {
	Set<IndexLocation> set = new HashSet<IndexLocation>();
	
	List<Object> itemIgnores = new ArrayList<Object>();
	public void addIndexLocation(IndexLocation l){
		if(l.policy.equals("i")){
			this.addItemIgnore(l.getRef());
		}else{
			set.add(l);
		}
		
	}
	
	public String  match(Object key){
		StringBuilder sb = new StringBuilder();
		Iterator<IndexLocation> it = set.iterator();
		while(it.hasNext()){
			IndexLocation l = it.next();
			if(l.getRef().equals("*")){
				sb.append(l.getPolicy()).append(",");
			}
			if(l.getRef().equals(key)){
				sb.append(l.getPolicy()).append(",");
			}
		}
		if(sb.length()!=0){
			sb.setLength(sb.length()-1);
			return sb.toString();
		}else{
			return null;
		}
		
	}
	public boolean hasRemovedItem(){
		return itemIgnores.size()!=0;
	}
	
	public void addItemIgnore(Object key){
		itemIgnores.add(key);
	}
	
	public boolean matchIgnoreKey(Object key){
		return itemIgnores.contains(key);
	}
}
